//http://api.ahjdev.top/execute

import Vue from 'vue';
import axios from 'axios';
import qs from 'qs';

// const url = 'http://api.ahjdev.top/execute/';//打包
var baseURL = '';    //本地

switch(process.env.TYPE){
    case 'dev':
        baseURL = 'https://api.ahjdev.top/execute/'
        break;
    case 'test':
        baseURL = 'https://api.ahjtest.top/execute/'
        break;
    case 'prod':
        baseURL = 'https://api.anhuanjia.com/execute/'
        break;
    default:
        baseURL = '/execute'
}

var getCookie = function (key) {
    if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(key + '=');
        if (start !== -1) {
            start = start + key.length + 1;
            var end = document.cookie.indexOf(';', start);
            if (end === -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(start, end));
        }
    }
    return '';
};
// 响应时间
axios.defaults.timeout = 10 * 1000;
// 配置cookie
axios.defaults.withCredentials = true;

// 配置请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
// 静态资源
Vue.prototype.$static = '';
// 配置接口地址
// axios.defaults.baseURL = 'http://192.168.50.110';
// POST传参序列化(添加请求拦截器)
axios.interceptors.request.use(
    config => {
        if (config.method === 'post') {
            config.data = qs.stringify(config.data);
        }
        return config;
    },
    err => {
        // Message.error('请求错误');
        return Promise.reject(err);
    }
);
// 返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
    res => {
        return res
    },
    err => {
        // Message.error('请求失败，请稍后再试');
        return Promise.reject(err);
    }
);

export function get (url, params) {
    return new Promise((resolve, reject) => {
        axios.defaults.baseURL = baseURL;
        axios
            .get(url, {
                params: params
            })
            .then(res => {
                resolve(res);
            })
            .catch(err => {
                reject(err);
            });
    });
}

export function post (method, params) {
    // debugger;
    let dc = {_METHOD: method, _DATA: JSON.stringify(params)};
    return new Promise((resolve, reject) => {
        axios.post(baseURL, dc)
            .then(
                res => {
                    console.log(res)
                   // eval(res.data._SCRIPT);
                    resolve(res);
                }
            )
            .catch(err => {
                reject(err);
            });
    });
};
export function fetchGrid (method, data, pageIndex, pageSize) {
    data._METHOD = method;
    data.pageIndex = pageIndex;
    data.pageSize = pageSize;
    let dc = {_METHOD: 'DataGridExecutor.fetch', _DATA: JSON.stringify(data)};
    return new Promise((resolve, reject) => {
        axios.post(url, dc)
            .then(
                res => {
                    //eval(res.data._SCRIPT);
                    resolve(res);
                },
                err => {
                    reject(err);
                }
            )
            .catch(err => {
                reject(err);
            });
    });
};
